<%--管理员-博客管理列表--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <meta charset="utf-8">
    <title>图形图像处理交流网站</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/admin/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/admin/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
            <%--表格--%>
            <table class="layui-hide" id="test" lay-filter="test"></table>

            <%--左上角工具栏--%>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm"  lay-event="add" id="test7"><i class="layui-icon">&#xe681;</i>上传图片</button>
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckData"><i class="layui-icon">&#xe640;</i>批量删除</button>
                </div>
            </script><%--表格操作--%>
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
            <script type="text/html" id="imgtmp">
                <img src="${pageContext.request.contextPath}{{d.lPath}}" style="width: 108px;height: 50px">
            </script>
                <%--这里是弹出层的表单信息 表单的id用于表单的选择，style是在本页隐藏，只有点击添加才会弹出--%>
                <div class="layui-row" id="addImg" style="display:none;">

                    <div class="layui-form layui-form-pane">
                        <center>
                            <form:form class="layui-form" action="" method="post">
                                <div class="layui-form-item">
                                </div>
                                <div class="layui-form-item">
                                    <label for="lName" class="layui-form-label">名称</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="lName" name="lName" required lay-verify="required" autocomplete="off" class="layui-input">
                                        <input type="hidden" id="lPath" name="lPath">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <button type="button" class="layui-btn  layui-btn-sm" id="uploadImg"><i class="layui-icon">&#xe681;</i>上传图片</button>
                                </div>

                                <div class="layui-form-item">
                                    <button class="layui-btn"  lay-filter="addForm" lay-submit>立即添加</button>
                                </div>
                            </form:form>
                        </center>
                    </div>
                    <div id="LAY_page1"></div>
                </div>
    </div>
</div>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.3.1.js" ></script>
<script src="${pageContext.request.contextPath}/admin/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>

<%--表格渲染--%>
<script>
    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem: '#test'
            ,url:'${pageContext.request.contextPath}/lunbo/teacher/admin_list.do'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            , id: 'test'
            ,title: '轮播图显示列表'
            ,cols: [[
                { fixed: 'left',type: 'checkbox',width:'10%'}
                ,{field:'lId', title:'ID',type:"numbers", width:'10%', unresize: true, align:'center'}
                ,{field:'lName', title:'标题', width:'15%', align:'center'}
                ,{field:'user', title:'上传用户', width:'15%', align:'center',templet: '<div>{{d.user.userName}}</div>' ,align:'center'}
                ,{field:'lcreateTime', title:'上传时间', width:'20%', align:'center'}
                ,{field:'lPath', title:'图片', width:'20%', align:'center',templet: "#imgtmp"}
                ,{title:'操作', toolbar: '#barDemo', width:'10%', align:'center'}
            ]]
            ,page: true
            ,done:function(res,curr,count) {
                hoverOpenImg();//显示大图
                $('table tr').on('click',function(){
                    $('table tr').css('background','');
                });
            }
            ,text: {
                none: '暂无相关数据'
            }
        });
        //数据表格重载
        // 执行搜索，表格重载
        var $ = layui.$,active={
            reload:function(){
                var demoReload = $('#demoReload').val();
                var startTime = $('#startTime').val();
                var endTime = $('#endTime').val();
                table.reload('test', {
                    method: 'get'
                    , where: {
                        'keywords': demoReload
                        ,'startTime':startTime
                        ,'endTime':endTime
                        ,'time':new Date().valueOf()
                    }
                    , page: {
                        curr: 1
                    }
                });
            }
        }

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData'://批量删除
                    var data = checkStatus.data;
                    if(data.length!=0){
                        var ids = [];//用户id
                        var lName=[];//用户名
                        for (var i = 0; i < data.length; i++) { //循环筛选出id
                            ids.push(data[i].lId);
                            lName.push(data[i].lName);
                        }
                        //发送删除请求
                        layer.confirm("确认要删除"+JSON.stringify(lName)+"吗，删除后不能恢复", { title: "删除确认" }, function(index){
                            senddel(ids);
                            return false;
                        });
                    }else{
                        layer.msg("您还未选择数据！",{icon: 5});
                    }
                    break;
                case "add":
                    layer.open({
                        //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                        type: 1,
                        title: "上传图片",
                        area: ['500px', '350px'],
                        content: $("#addImg")//引用的弹出层的页面层的方式加载上传图片表单
                    });
                    break;

            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    var ids=data.lId;
                    senddel(ids);
                    return false;
                });
            }else if(obj.event === 'detail'){
                window.location.href="${pageContext.request.contextPath}/blog/to_home.do?lId="+data.lId;
            }
        });

        function senddel(ids){
            $.ajax({
                url: "${pageContext.request.contextPath}/lunbo/teacher/admin_delete.do",
                type: "post",
                dataType:'json',
                data: 'ids='+ids,
                success: function (result) {
                    if (result.msg == 200) {
                        layer.msg('删除成功');
                        table.reload('test',{});
                    }else{
                        layer.msg(result.msg);
                    }
                }
            });
        }

    });
    function hoverOpenImg(){
        var img_show = null; // tips提示
        $('td img').hover(function(){
            var kd=$(this).width();
            kd1=kd*5;          //图片放大倍数
            kd2=kd*5+30;       //图片放大倍数
            var img = "<img class='img_msg' src='"+$(this).attr('src')+"' style='width:"+kd1+"px;' />";
            img_show = layer.tips(img, this,{
                tips:[4, 'rgba(41,41,41,.5)']
                ,area: [kd2+'px']
            });
        },function(){
            layer.close(img_show);
        });
        $('td img').attr('style','max-width:70px;display:block!important');
    }

</script>
<%--上传轮播图--%>
<script>

    layui.use('upload', function(){
        console.log("进入use");
        var $ = layui.jquery
            ,upload = layui.upload;
        //上传轮播图
        upload.render({
            elem: '#uploadImg'
            ,url: '${pageContext.request.contextPath}/lunbo/teacher/uploadImg.do'
            ,done: function(res){
                //如果上传失败
                if(res.code == 0){
                    return layer.msg('上传失败！');
                }
                //上传成功
                if(res.code > 0){
                    layer.msg('上传成功！')
                    //设置隐藏域的值
                    console.log("设置隐藏域的值--"+lPath);
                    $("#lPath").val(res.lPath);
                }
            }

        });

    });
</script>
<%--提交表单--%>
<script>
    layui.use(['layer','form'],function(){
        var form=layui.form;
        var layer=layui.layer;
        form.on('submit(addForm)', function(data){
            $.ajax({
                url:'${pageContext.request.contextPath}/admin/lunbo/add.do',
                method:'post',
                data:data.field,
                dataType:'JSON',
                success:function(res){
                    switch(res.code){
                        case 200:
                            var index = parent.layer.getFrameIndex(window.name);
                            layer.msg(res.msg, {icon: 1});
                            parent.layui.table.reload('test');//重载父页表格，参数为表格ID
                            parent.layer.close(index);
                            break;
                        case -1:
                            parent.layer.msg(res.msg, {icon: 2});
                            break;
                        case -2:
                            //登录失效
                            alert('登录失效');
                            window.open("${pageContext.request.contextPath}/user/to_login.do");
                            break;
                    }

                },
                error:function (data) {

                }
            }) ;

        });
    });
</script>
</body>
</html>
